package auto.app.util;

import io.jsonwebtoken.Claims;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class UserGetAccount {

    /**
     * 获取当前用户id
     * @return
     */
    public static Integer getAccountId(){
        ServletRequestAttributes servletRequestAttributes =  (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String header = request.getHeader("X-Token");
        Claims claims = JwtTemplate.parseJwt(header);
        return (Integer)claims.get("accountid");
    }

    /**
     * 获取当前用户姓名
     * @return
     */
    public static String getAccountName(){
        ServletRequestAttributes servletRequestAttributes =  (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String header = request.getHeader("X-Token");
        Claims claims = JwtTemplate.parseJwt(header);
        return (String)claims.get("userName");
    }

}
